/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.text.representation.vo;

import co.edu.unal.bioingenium.kbmed.data.PhysicalDocument;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import java.io.Serializable;
import java.lang.String;
import java.util.Set;

/**
 *
 * @author Alejandro Riveros Cruz Riveros Cruz
 */
public class Document implements Serializable {
    //SerialUID

    private static final long serialVersionUID = 40911353565749028L;
    /**
     * The document name  
     */
    private String name;
    /**
     * The total number of words in this document
     */
    private int totalWords;
    /**
     * The list of words contained in this document and the count for each word Map<String, Count>
     */
    private TObjectIntMap<String> wordList;

    /**
     * 
     * @param physicalDocument
     */
    public Document(PhysicalDocument physicalDocument) {
        this.name = physicalDocument.getName();
        totalWords = 0;
        wordList = new TObjectIntHashMap<String>();
    }

    /**
     *
     * @param name 
     */
    public Document(String name) {
        this.name = name;
        totalWords = 0;
        wordList = new TObjectIntHashMap<String>();
    }

    /**
     *
     * @param word 
     */
    public void addWordOccurrence(String word) {
        if (wordList.containsKey(word)) {
            wordList.increment(word);
        } else {
            wordList.put(word, 1);
        }
        totalWords++;
    }

    /**
     *
     * @param word
     * @return  
     */
    public int getWordOccurrences(String word) {
        if (containsWord(word)) {
            return wordList.get(word);
        } else {
            return 0;
        }
    }

    /**
     *
     * @param word 
     * @return 
     */
    public boolean containsWord(String word) {
        if (wordList.containsKey(word)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     *
     * @param word 
     */
    public void removeWord(String word) {
        if (containsWord(word)) {
            int wordOcurrences = wordList.get(word);
            wordList.remove(word);
            totalWords -= wordOcurrences;
        }
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the numWords
     */
    public int getTotalWords() {
        return totalWords;
    }

    /**
     * @param numWords the numWords to set
     */
    public void setTotalWords(int numWords) {
        this.totalWords = numWords;
    }

    /**
     * @return the words
     */
    public TObjectIntMap<String> getWordList() {
        return wordList;
    }

    /**
     * @return the words
     */
    public Set<String> getWords() {
        return wordList.keySet();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Document other = (Document) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }
}
